﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ChinaDynastyTours.Controls;
using ChinaDynastyTours.Models;
using System.Threading;
using DevExpress.XtraEditors.DXErrorProvider;

namespace ChinaDynastyTours.Views
{
    public partial class SignBillAccountAddForm : DevExpress.XtraEditors.XtraForm
    {
        public DataTable _SignBillDataTable;
        private string _TourGroupID;

        public SignBillAccountAddForm(DataTable signBillDataTable,string tourGroupID)
        {
            InitializeComponent();

            _SignBillDataTable = signBillDataTable;
            scheduleGridControl.DataSource = TeamGroupCtrl.GetTourGroupSchedule(tourGroupID).Tables[0];
            _TourGroupID = tourGroupID;
            tbTourgroupID.EditValue = _TourGroupID;
        }

        private void SignBillAccountAddForm_Load(object sender, EventArgs e)
        {
            
            //初始化所属类型
            tbTypeBelong.Properties.Items.Add(ExpenseTypeBelongEnum.娱乐);
            tbTypeBelong.Properties.Items.Add(ExpenseTypeBelongEnum.餐单);
            tbTypeBelong.SelectedIndex = 0;

            InitValidationRules();
        }

        private void btnProviders_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            ProviderSelectedToolForm providerForm = new ProviderSelectedToolForm();
            providerForm._ProviderTypes = new ProviderTypeEnum[] { ProviderTypeEnum.景点, ProviderTypeEnum.演出, ProviderTypeEnum.餐厅 };

            if (providerForm.ShowDialog(this) == DialogResult.OK)
            {
                DevExpress.XtraEditors.ButtonEdit btnEdit = sender as DevExpress.XtraEditors.ButtonEdit;
                btnEdit.Tag = providerForm.ReturnProviderID;
                btnEdit.EditValue = providerForm.ReturnProviderName;
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {

            if (!dxValidationProvider.Validate()) return;

            //创建费用
            ExpenseEntity expense = new ExpenseEntity()
            {
                //ExpenseID = _ExpenseID,
                TourgroupID = _TourGroupID,
                TourgroupName = TeamGroupCtrl.GetTeamGroupNameById(_TourGroupID),
                TourGroupScheduleID = int.Parse(scheduleGridView.GetRowCellValue(scheduleGridView.FocusedRowHandle, "TourGroupScheduleID").ToString()),
                ProviderID = tbProviders.Tag == null ? -1 : int.Parse(tbProviders.Tag.ToString()),
                ProviderName = tbProviders.Text,
                ServiceName = "",
                Budget = tbBudget.EditValue==null?0:decimal.Parse(tbBudget.EditValue.ToString()),
                PayTotal = 0,
                ApplyPayTotal = 0,
                AdjustAmount = 0,
                Status = ExpenseStatusEnum.计划.ToString(),
                TypeBelong = tbTypeBelong.EditValue.ToString(),
                ApplyPay = 0,
                PayStatus = PayApplyStatusEnum.未付.ToString(),
                CreateByUserName = Thread.CurrentPrincipal.Identity.Name //UserInfoCtrl.GetNameByUserName(Thread.CurrentPrincipal.Identity.Name)
            };

            EntertainmenRestaurantOrderEntity orderEntity = new EntertainmenRestaurantOrderEntity()
            {
                OrderState = OrderStateEnum.未订.ToString(),/*重点内容，初始化订单状态为‘未定’*/
                TourgroupID = _TourGroupID,
                TourgroupName = TeamGroupCtrl.GetTeamGroupNameById(_TourGroupID),

                ProviderID = tbProviders.Tag == null ? -1 : int.Parse(tbProviders.Tag.ToString()),
                ProviderName = tbProviders.Text,
                ServiceName = "",
                ContractPath = "",

                LocalPlanScheduleID = -1,
                ServiceDate = DateTime.Parse("1900-01-01").ToString("yyyy-MM-dd"),
                AdultUnitPrice = decimal.Parse(tbAdultUnitPrice.EditValue.ToString()),
                AccompanyUnitPrice = decimal.Parse(tbAccompanyUnitPrice.EditValue.ToString()),
                AccompanyNum = int.Parse(tbAccompanyNum.EditValue.ToString()),
                AdultNum = int.Parse(tbAdultNum.EditValue.ToString()),
                ChildNum = 0,
                ChildUnitPrice = 0,
                OthersCost = 0,
                CiceroniName = "",
                CiceroniPhone = "",
                Remark = "导游报销数据",

            };
            EntertainmenRestaurantOrderCtrl.CreateEntertainmenRestaurantOrderAndExpenseData(expense, orderEntity);
            
            
            //网格显示
            //DataRow dr = _SignBillDataTable.NewRow();
            //dr["TourgroupID"] = _TourGroupID;
            //dr["Status"] = ExpenseStatusEnum.计划;
            //dr["AdultNum"] = tbAdultNum.EditValue;
            //dr["AdultUnitPrice"] = tbAdultUnitPrice.EditValue;
            //dr["AccompanyNum"] = tbAccompanyNum.EditValue;
            //dr["AccompanyUnitPrice"] = tbAccompanyUnitPrice.EditValue;
            //dr["Budget"] = tbBudget.EditValue;
            //dr["ProviderID"] = tbProviders.Tag;
            //dr["ProviderName"] = tbProviders.EditValue;
            //dr["TypeBelong"] = tbTypeBelong.EditValue;
            //dr["CreateByUserName"] = Thread.CurrentPrincipal.Identity.Name;

            //_SignBillDataTable.Rows.Add(dr);

        }

        private void btnCalculationPrice_Click(object sender, EventArgs e)
        {
            tbBudget.EditValue = int.Parse(tbAdultNum.Text) * decimal.Parse(tbAdultUnitPrice.EditValue.ToString()) +
                                     int.Parse(tbAccompanyNum.Text) * decimal.Parse(tbAccompanyUnitPrice.EditValue.ToString());

        }

        /// <summary>
        /// 控件数据校验
        /// </summary>
        private void InitValidationRules()
        {
            // <notEmptyTextEdit> 
            ConditionValidationRule notEmptyValidationRule = new ConditionValidationRule();
            notEmptyValidationRule.ConditionOperator = ConditionOperator.IsNotBlank;
            notEmptyValidationRule.ErrorText = "不能为空";
            // </notEmptyTextEdit>

            //不能为空
            dxValidationProvider.SetValidationRule(tbProviders, notEmptyValidationRule);
            dxValidationProvider.SetValidationRule(tbTypeBelong, notEmptyValidationRule);

        }
    }
}
